package com.example.dilidili_ting.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.dilidili_ting.R;
import com.example.dilidili_ting.activity.MovieDetailsActivityTing;
import com.example.dilidili_ting.activity.MovielistActivityTing;
import com.example.dilidili_ting.bean.MovieBeanTing;
import com.google.gson.Gson;

import java.util.List;

public class MovielistAdapterTing extends BaseAdapter {
    
    private Context contextTing;
    private List<MovieBeanTing> movieBeanTing;

    public MovielistAdapterTing(Context context, List<MovieBeanTing> movieBean){
        this.contextTing = context;
        this.movieBeanTing = movieBean;
    }
    static class ViewHolder{
        private ImageView ivImgTing;
        private TextView tvTitleTing;
        private TextView tvDirectorTing;
        private TextView tvCastTing;
        private TextView yvTypeTing;
        private TextView tvCountryTing;
        private LinearLayout llMovieListTing;
    }
    
    @Override
    public int getCount() {
        return movieBeanTing == null?0:movieBeanTing.size();
    }

    @Override
    public MovieBeanTing getItem(int position) {
        return movieBeanTing == null?null:movieBeanTing.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder vh;
        if (convertView==null){
            vh = new ViewHolder();
            convertView = LayoutInflater.from(contextTing).inflate(R.layout.movie_list_item_ting,parent,false);
            vh.ivImgTing = (ImageView) convertView.findViewById(R.id.iv_img_ting);
            vh.tvTitleTing = (TextView) convertView.findViewById(R.id.tv_title_ting);
            vh.tvDirectorTing = (TextView) convertView.findViewById(R.id.tv_director_ting);
            vh.tvCastTing = (TextView) convertView.findViewById(R.id.tv_cast_ting);
            vh.yvTypeTing = (TextView) convertView.findViewById(R.id.yv_type_ting);
            vh.tvCountryTing = (TextView) convertView.findViewById(R.id.tv_country_ting);
            vh.llMovieListTing = (LinearLayout) convertView.findViewById(R.id.ll_movie_list_ting);
            convertView.setTag(vh);
        }else {
            vh= (ViewHolder) convertView.getTag();
        }
        final MovieBeanTing movielist = getItem(position);
        if(movielist != null){
            //加载网络图片
            RequestOptions options = new RequestOptions().error(R.drawable.k8).bitmapTransform(new RoundedCorners( 10 ));//图片圆角为10
            Glide.with(vh.ivImgTing.getContext()).load(movieBeanTing.get(position).getMovieCover()).apply(options).into(vh.ivImgTing);
            vh.tvTitleTing.setText(movieBeanTing.get(position).getMovieName());
            vh.tvCastTing.setText(movieBeanTing.get(position).getCast());
            vh.tvDirectorTing.setText(movieBeanTing.get(position).getDirector());
            vh.tvCastTing.setText(movieBeanTing.get(position).getCast());
            vh.yvTypeTing.setText(movieBeanTing.get(position).getType());
            vh.tvCountryTing.setText(movieBeanTing.get(position).getCountry()+" "+movieBeanTing.get(position).getReleaseYear());
            vh.llMovieListTing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // TODO: 2021/6/17 调接口传入信息,获取电影详情,并跳转页面
                    Intent intent = new Intent(contextTing, MovieDetailsActivityTing.class);
                    intent.putExtra("movieName", movieBeanTing.get(position).getMovieName());
                    contextTing.startActivity(intent);
                }
            });
        }
//        Log.i("hhha",new Gson().toJson(movieBeanTing));
        return convertView;
    }
}
